package cn.tedu.logistics.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.tedu.logistics.bean.Depository;
import cn.tedu.logistics.service.DepositoryService;

@Controller
public class DepositoryController {
	
	@Autowired
	private DepositoryService depoService;


	@RequestMapping("toDepository")
	public String toDepository() {//跳转到depository页面
		return "depository";
	}
	
	@RequestMapping("selectByShopId")
	public String selectByDepositoryInfoId(String shopId,ModelMap map) {
		List<Depository> list = depoService.selectDepoByShopId(shopId);
		map.addAttribute("list",list);//将获取到的depo信息列表，命名为list，在jsp中接收
		return "depository";//跳转回页面
	}
	
	@RequestMapping(value="/depo_select.do",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String getById(String shopId) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		List<Depository> depos = null;
		String json = null;
		if(shopId!=null) {
			depos = depoService.selectDepoByShopId(shopId);
			json = mapper.writeValueAsString(depos);
		}
		return json;
	}
	
	@RequestMapping("toupdDepo")
	public String toUpdDepo(Long depositoryInfoId, ModelMap map) {//跳转到指定记录的修改页面
		Depository depo = depoService.selectById(depositoryInfoId);
		map.addAttribute("depo", depo);	
		return "updDepo";
	}
	
	@RequestMapping("updDepo")
	public String updDepo(Depository depo) {//实现修改
		if(depo!=null) {
			depoService.updDepository(depo);
		}
		return "depository";//跳转回库存查询页面，考虑如何记住shopId
	}
	
	

}
